package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import utils.Utils;

/**
 * Created by ASUS on 2017/11/6.
 */

public class QQAdapter extends BaseAdapter {
    private List<String> qqList;
    private Context mContext;

    public QQAdapter(List<String> qqList, Context context){
        this.qqList = qqList;
        mContext = context;
    }

    @Override
    public int getCount() {
        if(qqList == null){
            return 0;
        }
        return qqList.size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.qq_item, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        holder.number.setText(qqList.get(i));
        holder.click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Utils.isQQClientAvailable(mContext)){
                    final String qqUrl = String.format("mqqwpa://im/chat?chat_type=wpa&uin=%s&version=1", qqList.get(i));
                    mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(qqUrl)));
                } else{
                    Utils.showToast("请安装QQ客户端");
                }
            }
        });

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.number)
        TextView number;
        @BindView(R.id.click)
        TextView click;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
